package com.liuhongwei.test.builder.strategy.impl;


import com.liuhongwei.test.bean.FieldInfo;
import com.liuhongwei.test.bean.TableInfo;
import com.liuhongwei.test.builder.BuildExtendMapperXml;
import com.liuhongwei.test.builder.strategy.inf.MapperXmlStrategy;

import java.io.BufferedWriter;
import java.io.IOException;

/**
 * 生成实体映射文件
 */
public class XmlFieldResultMapImpl implements MapperXmlStrategy {

    @Override
    public void generateMapperXml(TableInfo tableInfo, BufferedWriter bw) throws IOException {
        String po = BuildExtendMapperXml.getPo(tableInfo);

        bw.write("\t<!--实体映射-->\n");
        bw.write("\t<resultMap id=\"" + BuildExtendMapperXml.BASE_RESULT_MAP + "\" type=\"" + po + "\">\n");
        for (FieldInfo field : tableInfo.getFieldList()) {
            bw.write("\t\t<!--" + field.getComment() + "-->\n");
            String key = "result";
            if (field.getAutoIncrement()) {
                key = "id";
            }
            String fieldName = field.getFieldName();
            String propertyName = field.getPropertyName();
            bw.write("\t\t<" + key + " column=\"" + fieldName + "\" property=\"" + propertyName + "\"/>\n");
        }
        bw.write("\t</resultMap>" + "\n\n");
    }
}
